<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box1 {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: relative;
        }
    </style>
</head>

<body>
    <div id="box1"></div>
    <script>
        window.onload = function () {
            /**
             * 使div可以根据方向键移动
            */
           var box1 = document.getElementById("box1")
            document.onkeydown = function (even) {
                even = even || window.even;
                /**
                 * 37左、38上、39右、40下
                */
                console.log(even.keyCode);

                var spead = 10;
                if(even.ctrlKey){
                    spead = 50;
                }
                switch (even.keyCode) {
                    case 37:
                        console.log("向左");
                        box1.style.left = box1.offsetLeft - spead+"px"
                        break;
                    case 38:
                        console.log("向上");
                        box1.style.top = box1.offsetTop - spead+"px"
                        break;
                    case 39:
                        console.log("向右");
                        box1.style.left = box1.offsetLeft + spead+"px"
                        break;
                    case 40:
                        console.log("向下");
                        box1.style.top = box1.offsetTop + spead+"px"
                        break;
                    // default:
                    //     alert("请不要按除方向键以外的按键");
                }

            }




        }


    </script>
</body>

</html>